﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using WebVideo.Areas.Admin.Models;
using MvcLibrary.Repository;

namespace WebVideo.Areas.Admin.Controllers
{
    public class FAQController : Controller
    {
        //
        // GET: /Admin/FAQ/

        public ActionResult Index()
        {
            return View(FAQModel.Index());
        }

        #region "Insert"
        public ActionResult Insert()
        {
            FAQModel tintuc = new FAQModel();
            return View(tintuc);
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Insert(FAQModel FAQ)
        {

            try
            {
                if (ModelState.IsValid)
                {
                    FAQModel.Insert(FAQ);
                    return RedirectToAction("Index", "FAQ");
                }
            }
            catch
            {

                return RedirectToAction("Index", "Error", new { errorMsg = "Lỗi SQL hosting / server" });
            }
            return View(FAQ);
        }
        #endregion //insert

        #region "Delete"

        public string Delete(int Id)
        {
            try
            {
                FAQModel.Delete(Id);
                return "ok";
            }
            catch
            {
                return "error";
            }
        }
        #endregion //Delete

        #region "Edit"
        public ActionResult Edit(int IdFAQ)
        {


            return View(FAQModel.LayModel(IdFAQ));

        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(FAQModel FAQ)
        {

            try
            {
                if (ModelState.IsValid)
                {
                    FAQModel.Edit(FAQ);
                    return RedirectToAction("Index", "FAQ");
                }
            }
            catch
            {

                return RedirectToAction("Index", "Error", new { errorMsg = "Lỗi SQL hosting / server" });
            }
            return View(FAQ);
        }
        #endregion //Edit


        public ActionResult IndexS()
        {

            return View(SiteInfoModel.Index());
        }

        #region "Edit"
        public ActionResult EditS(int Id)
        {

            return View(SiteInfoModel.LayModel(Id));

        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult EditS(SiteInfoModel Mtnew, HttpPostedFileBase Image)
        {

            try
            {
                if (ModelState.IsValid)
                {
                    if (Image != null && Image.ContentLength > 0)
                    {
                        int kb = Image.ContentLength / 1024; //file size kb
                        if (kb >= 2048) // file qua lon
                        {
                            return RedirectToAction("Index", "Error", new { errorMsg = "Images must be less than 2MB." });
                        }

                        ImageHelper imgHelper = new ImageHelper();
                        string encodestring = imgHelper.encodeImageFile(Image);

                        if (encodestring == "!")
                            return RedirectToAction("Index", "Error", new { errorMsg = "Can't upload Images" });

                        var path = Path.Combine(Server.MapPath("~/Content/Images/SiteInfo"), encodestring);
                        Image.SaveAs(path);

                        Mtnew.Image = encodestring;
                    }

                    string image = SiteInfoModel.Edit(Mtnew);
                    if (image != null)
                    {
                        string fileToDelete = Path.Combine(Server.MapPath("~/Content/Images/SiteInfo"), image); // file hinh cu
                        System.IO.File.Delete(fileToDelete);
                    }
                    return RedirectToAction("IndexS", "FAQ");
                }
            }
            catch
            {

                return RedirectToAction("Index", "Error", new { errorMsg = "Lỗi SQL hosting / server" });
            }
            return View(Mtnew);
        }
        #endregion //Edit

    }
}
